package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.UserMapper;
import com.rivulet.foreign.mapper.VacationMapper;
import com.rivulet.foreign.pojo.Vacation;
import com.rivulet.foreign.service.VacationService;
import com.rivulet.foreign.tools.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Service
public class VacationServiceImpl implements VacationService {
    final
    VacationMapper vacationMapper;
    final
    UserMapper userMapper;

    public VacationServiceImpl(VacationMapper vacationMapper, UserMapper userMapper) {
        this.vacationMapper = vacationMapper;
        this.userMapper = userMapper;
    }

    @Override
    public Results checkMyVacation(String student_id) {
        Results results = new Results(100, null);
        try {
            Vacation vacation = vacationMapper.getVacationById(student_id);
        } catch (Exception e) {
            results.setCode(404);
        }
        return results;
    }

    @Override
    public Results checkYetVacation() {
        Results<List<Vacation>> results = new Results(100, null);
        try {
            List<Vacation> list = vacationMapper.getAllYetVacation();
            results.setData(list);
        } catch (Exception e) {
            System.out.println(e.toString());
            results.setCode(404);
        }
        return results;
    }

    @Override
    public Results setVacation(Vacation vacation) {
        Results results = new Results(100, null);
        try {
            String classes = userMapper.getClassesById(vacation.getUser_id());
            Date begin = vacation.getBegin_time();
            Date end = vacation.getEnd_time();
            long duration_time = (end.getTime() - begin.getTime()) / (3600 * 24 * 1000);
            if (classes != null) {
                if (vacationMapper.getVacationById(vacation.getUser_id()) != null) {
                    vacationMapper.delVacation(vacation.getUser_id());
                }
                vacation.setClasses(classes);
                vacationMapper.setVacation(vacation.getUser_id(), vacation.getClasses(), vacation.getReason(), vacation.getBegin_time(),
                        vacation.getEnd_time(), (int) duration_time, vacation.getDestination());
                results.setMsg("事假申请成功，请返回首页留意请假进度");
            } else {
                results.setCode(400);
                results.setMsg("事假申请失败，请先填写个人信息");
            }
        } catch (Exception e) {
            results.setCode(404);
            results.setMsg("事假申请异常，请刷新重试，或联系管理员");
        }
        return results;
    }

    @Override
    public Results dealVacation(String user_id, Vacation vacation) {
        Results results = new Results(404, null);
        try {
            Integer identity = userMapper.getUserById(user_id).getIdentity();
            switch (identity) {
                case 5:
                    vacationMapper.setCounselorAdvice(vacation.getUser_id(), vacation.getIs_yes());
                    break;
                case 6:
                    vacationMapper.setManagerAdvice(vacation.getUser_id(), vacation.getIs_yes());
                    break;
                case 7:
                    vacationMapper.setCollegeAdvice(vacation.getUser_id(), vacation.getIs_yes());
                    break;
                default:;
            }
            vacationMapper.setIsYes(vacation.getUser_id(), vacation.getIs_yes());
            results.setCode(100);
            results.setMsg("审批完成！");
        } catch (Exception e) {
            results.setMsg("审核异常，请重试！");
        }
        return results;
    }
}
